<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="google-site-verification" content="yF8HXsFuCTrePDFRnSkN2kkzV2ypBjtywzXsfVXOOV8">
  <meta name="baidu-site-verification" content="VxrVAiQAZ9">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yang0033.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":true,"preload":true},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="本文接 《如何使用iOS实现《芈月传》的直播、点播？-HTTP Live Streaming (HLS)（一）》 HLS-Demo地址：https:&#x2F;&#x2F;github.com&#x2F;yangchao0033&#x2F;HLS-Demo 使用demo前请注意下面的问题，我在代码中也做了注释。 1234567891011&#x2F;&#x2F;#warning 注意，不要直接使用切换流的主索引，当前代码的协议只提供对.ts定位的子索引的下">
<meta property="og:type" content="article">
<meta property="og:title" content="如何使用iOS实现《芈月传》的直播、点播、缓存？-HTTP-Live-Streaming-(HLS)（二）">
<meta property="og:url" content="http://yang0033.gitee.io/2016/02/15/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8iOS%E5%AE%9E%E7%8E%B0%E3%80%8A%E8%8A%88%E6%9C%88%E4%BC%A0%E3%80%8B%E7%9A%84%E7%9B%B4%E6%92%AD%E3%80%81%E7%82%B9%E6%92%AD%E3%80%81%E7%BC%93%E5%AD%98%EF%BC%9F-HTTP-Live-Streaming-(HLS)%EF%BC%88%E4%BA%8C%EF%BC%89/index.html">
<meta property="og:site_name" content="SuperYang&#96;s Blog">
<meta property="og:description" content="本文接 《如何使用iOS实现《芈月传》的直播、点播？-HTTP Live Streaming (HLS)（一）》 HLS-Demo地址：https:&#x2F;&#x2F;github.com&#x2F;yangchao0033&#x2F;HLS-Demo 使用demo前请注意下面的问题，我在代码中也做了注释。 1234567891011&#x2F;&#x2F;#warning 注意，不要直接使用切换流的主索引，当前代码的协议只提供对.ts定位的子索引的下">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://github.com/yangchao0033/HLS-Demo/blob/master/%E9%85%8D%E7%BD%AE%E7%8E%AF%E5%A2%831.png?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/HLS-Demo/blob/master/%E9%85%8D%E7%BD%AE%E7%8E%AF%E5%A2%832.png?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/yangchao0033.github.io/blob/source/images/ios/2016/2/HLS_demo_UI.png?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/yangchao0033.github.io/blob/source/images/ios/2016/2/HLS%E5%8D%8F%E8%AE%AE%E6%96%87%E4%BB%B6%E5%AD%98%E5%82%A8%20.png?raw=true">
<meta property="article:published_time" content="2016-02-15T05:32:00.000Z">
<meta property="article:modified_time" content="2020-07-09T07:59:04.883Z">
<meta property="article:author" content="SuperYang">
<meta property="article:tag" content="HLS">
<meta property="article:tag" content="直播">
<meta property="article:tag" content="点播">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://github.com/yangchao0033/HLS-Demo/blob/master/%E9%85%8D%E7%BD%AE%E7%8E%AF%E5%A2%831.png?raw=true">

<link rel="canonical" href="http://yang0033.gitee.io/2016/02/15/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8iOS%E5%AE%9E%E7%8E%B0%E3%80%8A%E8%8A%88%E6%9C%88%E4%BC%A0%E3%80%8B%E7%9A%84%E7%9B%B4%E6%92%AD%E3%80%81%E7%82%B9%E6%92%AD%E3%80%81%E7%BC%93%E5%AD%98%EF%BC%9F-HTTP-Live-Streaming-(HLS)%EF%BC%88%E4%BA%8C%EF%BC%89/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>如何使用iOS实现《芈月传》的直播、点播、缓存？-HTTP-Live-Streaming-(HLS)（二） | SuperYang`s Blog</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-71812998-3"></script>
    <script>
      if (CONFIG.hostname === location.hostname) {
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'UA-71812998-3');
      }
    </script>


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?8ff3295288514de6f89f314c23ee40ab";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">SuperYang`s Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">落魄程序员在线炒饭</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-message">

    <a href="/message/" rel="section"><i class="fa fa-music fa-fw"></i>留言板 | Music</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yang0033.gitee.io/2016/02/15/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8iOS%E5%AE%9E%E7%8E%B0%E3%80%8A%E8%8A%88%E6%9C%88%E4%BC%A0%E3%80%8B%E7%9A%84%E7%9B%B4%E6%92%AD%E3%80%81%E7%82%B9%E6%92%AD%E3%80%81%E7%BC%93%E5%AD%98%EF%BC%9F-HTTP-Live-Streaming-(HLS)%EF%BC%88%E4%BA%8C%EF%BC%89/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://tva2.sinaimg.cn/crop.259.17.290.290.180/c2abdfa9jw8ez7appr3p2j20g40a2t91.jpg?KID=imgbed,tva&Expires=1594186820&ssig=BdxhDdi1Ti">
      <meta itemprop="name" content="SuperYang">
      <meta itemprop="description" content="牛肉炒饭不要香菜，老板收钱">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="SuperYang`s Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          如何使用iOS实现《芈月传》的直播、点播、缓存？-HTTP-Live-Streaming-(HLS)（二）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2016-02-15 13:32:00" itemprop="dateCreated datePublished" datetime="2016-02-15T13:32:00+08:00">2016-02-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-07-09 15:59:04" itemprop="dateModified" datetime="2020-07-09T15:59:04+08:00">2020-07-09</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a>
                </span>
            </span>

          
            <span id="/2016/02/15/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8iOS%E5%AE%9E%E7%8E%B0%E3%80%8A%E8%8A%88%E6%9C%88%E4%BC%A0%E3%80%8B%E7%9A%84%E7%9B%B4%E6%92%AD%E3%80%81%E7%82%B9%E6%92%AD%E3%80%81%E7%BC%93%E5%AD%98%EF%BC%9F-HTTP-Live-Streaming-(HLS)%EF%BC%88%E4%BA%8C%EF%BC%89/" class="post-meta-item leancloud_visitors" data-flag-title="如何使用iOS实现《芈月传》的直播、点播、缓存？-HTTP-Live-Streaming-(HLS)（二）" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2016/02/15/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8iOS%E5%AE%9E%E7%8E%B0%E3%80%8A%E8%8A%88%E6%9C%88%E4%BC%A0%E3%80%8B%E7%9A%84%E7%9B%B4%E6%92%AD%E3%80%81%E7%82%B9%E6%92%AD%E3%80%81%E7%BC%93%E5%AD%98%EF%BC%9F-HTTP-Live-Streaming-(HLS)%EF%BC%88%E4%BA%8C%EF%BC%89/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2016/02/15/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8iOS%E5%AE%9E%E7%8E%B0%E3%80%8A%E8%8A%88%E6%9C%88%E4%BC%A0%E3%80%8B%E7%9A%84%E7%9B%B4%E6%92%AD%E3%80%81%E7%82%B9%E6%92%AD%E3%80%81%E7%BC%93%E5%AD%98%EF%BC%9F-HTTP-Live-Streaming-(HLS)%EF%BC%88%E4%BA%8C%EF%BC%89/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4.9k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>4 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>本文接 <a href="http://www.jianshu.com/p/8f99202ccb89" target="_blank" rel="noopener">《如何使用iOS实现《芈月传》的直播、点播？-HTTP Live Streaming (HLS)（一）》</a></p>
<p>HLS-Demo地址：<a href="https://github.com/yangchao0033/HLS-Demo" target="_blank" rel="noopener">https://github.com/yangchao0033/HLS-Demo</a></p>
<p><strong>使用demo前请注意下面的问题，我在代码中也做了注释。</strong></p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//#warning 注意，不要直接使用切换流的主索引，当前代码的协议只提供对.ts定位的子索引的下载和播放，而且其中只有点播协议那一小段是可以下载的，直播协议只能播放，无法下载，无法下载的原因是因为m3u8的那个库中只对特定的一种m3u8的格式做了解析，而m3u8的格式有很多种，所以无法加息出来，该demo只做演示，不会对所有格式进行全解析，如果大家感兴趣的话可以对m3u8的库进行扩展，在github 上 pull request 我做一个补充扩展，我会及时在博客中进行更新。博客地址：superyang.gitcafe.io或yangchao0033.github.io 同简书：http://www.jianshu.com/users/f37a8f0ba6f8/latest_articles</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/** 点播协议 (只有这个是可以下载的，但是苦于太短，没办法播放出来，正在寻找可以下载并播放的新的点播或直播源,希望有读者可以帮忙提供哈，不甚感激~)*/</span></span><br><span class="line"><span class="comment">//#define TEST_HLS_URL @"http://m3u8.tdimg.com/147/806/921/3.m3u8"</span></span><br><span class="line"><span class="comment">/** 视频直播协议 */</span></span><br><span class="line"><span class="comment">/** 父索引(无法下载，只作为结构分析) */</span></span><br><span class="line"><span class="comment">//#define TEST_HLS_URL @"http://dlhls.cdn.zhanqi.tv/zqlive/34338_PVMT5.m3u8"</span></span><br><span class="line"><span class="comment">/** 子索引(无法下载，只作为结构分析) */</span></span><br><span class="line"><span class="comment">//#define TEST_HLS_URL @"http://dlhls.cdn.zhanqi.tv/zqlive/34338_PVMT5_1024/index.m3u8?Dnion_vsnae=34338_PVMT5"</span></span><br><span class="line"><span class="comment">/** wwcd视频，果然苹果自己就用这个协议(无法下载，只作为结构分析) */</span></span><br><span class="line"><span class="comment">//#define TEST_HLS_URL @"http://devstreaming.apple.com/videos/wwdc/2015/413eflf3lrh1tyo/413/hls_vod_mvp.m3u8"</span></span><br></pre></td></tr></table></figure>
<a id="more"></a> 

<p>如果觉得文章有用的话，请读者在github上点个star，或者在简书上点个赞。</p>
<p>本文在<strong>我的博客</strong>上同样有发布，欢迎各位读者留言。<br>博客地址:<a href="http://superyang.gitcafe.io/blog/2016/02/14/hls-2/" target="_blank" rel="noopener">http://superyang.gitcafe.io/blog/2016/02/14/hls-2/</a></p>
<p><strong>Demo配置原理：</strong></p>
<p>1、 需要导入第三方库：ASIHttpRequest，CocoaHTTPServer，m3u8（其中ASI用于网络请求，CocoaHTTPServer用于在ios端搭建服务器使用，m3u8是用来对返回的索引文件进行解析的）</p>
<p><img src="https://github.com/yangchao0033/HLS-Demo/blob/master/%E9%85%8D%E7%BD%AE%E7%8E%AF%E5%A2%831.png?raw=true" alt="ASI配置注意事项"></p>
<p><img src="https://github.com/yangchao0033/HLS-Demo/blob/master/%E9%85%8D%E7%BD%AE%E7%8E%AF%E5%A2%832.png?raw=true" alt="MRC报错处理"></p>
<p>2、导入系统库：libsqlite3.dylib、libz.dylib、libxml2.dylib、CoreTelephony.framework、SystemConfiguration.framework、MobileCoreServices.framework、Security.framework、CFNetwork.framework、MediaPlayer.framework</p>
<p>3、添加头文件</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">YCHLS-Demo.h</span><br></pre></td></tr></table></figure>

<p>4、demo介绍<br><img src="https://github.com/yangchao0033/yangchao0033.github.io/blob/source/images/ios/2016/2/HLS_demo_UI.png?raw=true" alt="demo样式"></p>
<ul>
<li><strong>播放：</strong>直接播放在线的直播链接，是由系统的MPMoviePlayer完成的，它自带解析HLS直播链的功能。</li>
<li><strong>下载：</strong>遵循HLS的协议，通过索引文件的资源路径下载相关的视频切片并保存到手机本地。</li>
<li><strong>播放本地视频：</strong>使用下载好的视频文件片段进行连续播放。</li>
<li><strong>清除缓存：</strong>删除下载好的视频片段</li>
</ul>
<p>原理：</p>
<ol>
<li>通过ASI请求链接，通过m3u8库解析返回的m3u8索引文件。</li>
<li>再通过ASI下载解析出的视频资源地址，仿照HLS中文件存储路径存储。</li>
<li>利用CocoaHTTPServer在iOS端搭建本地服务器，并开启服务，端口号为：12345（高位端口即可）。配置服务器路径与步骤二存储路径一致。</li>
<li>设置播放器直播链接为本地服务器地址，直接播放，由于播放器遵守HLS协议，所以能够解析我们之前使用HLS协议搭建的本地服务器地址。</li>
<li>点击在线播放，校验是否与本地播放效果一致。</li>
</ol>
<p><img src="https://github.com/yangchao0033/yangchao0033.github.io/blob/source/images/ios/2016/2/HLS%E5%8D%8F%E8%AE%AE%E6%96%87%E4%BB%B6%E5%AD%98%E5%82%A8%20.png?raw=true" alt="HLS协议文件存储结构"></p>
<p>上面是HLS中服务器存储视频文件切片和索引文件的结构图<br>整个流程就是：</p>
<ol>
<li>先点击下载，通过解析m3u8的第三方库解析资源。（m3u8的那个库只能解析一种特定格式的m3u8文件，代码里会有标注）</li>
<li>点击播放本地视频播放下载好的资源。</li>
<li>点击播放是用来预览直播的效果，与整个流程无关。</li>
<li>其中进度条用来显示下载进度。</li>
</ol>
<blockquote>
<p>总结：<br>整个Demo并不只是让我们搭建一个Hls服务器或者一个支持Hls的播放器。目的在于了解Hls协议的具体实现，以及服务器端的一些物理架构。通过Demo的学习，可以详细的了解Hls直播具体的实现流程。</p>
</blockquote>
<h2 id="部分源码贴出"><a href="#部分源码贴出" class="headerlink" title="部分源码贴出"></a>部分源码贴出</h2><p>开启本地服务器：</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="keyword">void</span>)openHttpServer</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">self</span>.httpServer = [[HTTPServer alloc] init];</span><br><span class="line">    [<span class="keyword">self</span>.httpServer setType:<span class="string">@"_http._tcp."</span>];  <span class="comment">// 设置服务类型</span></span><br><span class="line">    [<span class="keyword">self</span>.httpServer setPort:<span class="number">12345</span>]; <span class="comment">// 设置服务器端口</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 获取本地Library/Cache路径下downloads路径</span></span><br><span class="line">    <span class="built_in">NSString</span> *webPath = [kLibraryCache stringByAppendingPathComponent:kPathDownload];</span><br><span class="line">    <span class="built_in">NSLog</span>(<span class="string">@"-------------\\nSetting document root: %@\\n"</span>, webPath);</span><br><span class="line">    <span class="comment">// 设置服务器路径</span></span><br><span class="line">    [<span class="keyword">self</span>.httpServer setDocumentRoot:webPath];</span><br><span class="line">    <span class="built_in">NSError</span> *error;</span><br><span class="line">    <span class="keyword">if</span>(![<span class="keyword">self</span>.httpServer start:&amp;error])</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">NSLog</span>(<span class="string">@"-------------\\nError starting HTTP Server: %@\\n"</span>, error);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>视频下载：</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="keyword">IBAction</span>)downloadStreamingMedia:(<span class="keyword">id</span>)sender &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">UIButton</span> *downloadButton = sender;</span><br><span class="line">    <span class="comment">// 获取本地Library/Cache路径</span></span><br><span class="line">    <span class="built_in">NSString</span> *localDownloadsPath = [kLibraryCache stringByAppendingPathComponent:kPathDownload];</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 获取视频本地路径</span></span><br><span class="line">    <span class="built_in">NSString</span> *filePath = [localDownloadsPath stringByAppendingPathComponent:<span class="string">@"XNjUxMTE4NDAw/movie.m3u8"</span>];</span><br><span class="line">    <span class="built_in">NSFileManager</span> *fileManager = [<span class="built_in">NSFileManager</span> defaultManager];</span><br><span class="line">    <span class="comment">// 判断视频是否缓存完成，如果完成则播放本地缓存</span></span><br><span class="line">    <span class="keyword">if</span> ([fileManager fileExistsAtPath:filePath]) &#123;</span><br><span class="line">        [downloadButton setTitle:<span class="string">@"已完成"</span> forState:<span class="built_in">UIControlStateNormal</span>];</span><br><span class="line">        downloadButton.enabled = <span class="literal">NO</span>;</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        M3U8Handler *handler = [[M3U8Handler alloc] init];</span><br><span class="line">        handler.delegate = <span class="keyword">self</span>;</span><br><span class="line">        <span class="comment">// 解析m3u8视频地址</span></span><br><span class="line">        [handler praseUrl:TEST_HLS_URL];</span><br><span class="line">        <span class="comment">// 开启网络指示器</span></span><br><span class="line">        [[<span class="built_in">UIApplication</span> sharedApplication] setNetworkActivityIndicatorVisible:<span class="literal">YES</span>];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>播放本地视频：</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="keyword">IBAction</span>)playVideoFromLocal:(<span class="keyword">id</span>)sender &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">NSString</span> * playurl = [<span class="built_in">NSString</span> stringWithFormat:<span class="string">@"http://127.0.0.1:12345/XNjUxMTE4NDAw/movie.m3u8"</span>];</span><br><span class="line">    <span class="built_in">NSLog</span>(<span class="string">@"本地视频地址-----%@"</span>, playurl);</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 获取本地Library/Cache路径</span></span><br><span class="line">    <span class="built_in">NSString</span> *localDownloadsPath = [kLibraryCache stringByAppendingPathComponent:kPathDownload];</span><br><span class="line">    <span class="comment">// 获取视频本地路径</span></span><br><span class="line">    <span class="built_in">NSString</span> *filePath = [localDownloadsPath stringByAppendingPathComponent:<span class="string">@"XNjUxMTE4NDAw/movie.m3u8"</span>];</span><br><span class="line">    <span class="built_in">NSFileManager</span> *fileManager = [<span class="built_in">NSFileManager</span> defaultManager];</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 判断视频是否缓存完成，如果完成则播放本地缓存</span></span><br><span class="line">    <span class="keyword">if</span> ([fileManager fileExistsAtPath:filePath]) &#123;</span><br><span class="line">        <span class="built_in">MPMoviePlayerViewController</span> *playerViewController =[[<span class="built_in">MPMoviePlayerViewController</span> alloc]initWithContentURL:[<span class="built_in">NSURL</span> URLWithString: playurl]];</span><br><span class="line">        [<span class="keyword">self</span> presentMoviePlayerViewControllerAnimated:playerViewController];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="built_in">UIAlertView</span> *alertView = [[<span class="built_in">UIAlertView</span> alloc] initWithTitle:<span class="string">@"Sorry"</span> message:<span class="string">@"当前视频未缓存"</span> delegate:<span class="keyword">self</span> cancelButtonTitle:<span class="string">@"确定"</span> otherButtonTitles:<span class="literal">nil</span>, <span class="literal">nil</span>];</span><br><span class="line">        [alertView show];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>播放在线视频</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="keyword">IBAction</span>)playLiveStreaming &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">NSURL</span> *url = [[<span class="built_in">NSURL</span> alloc] initWithString:TEST_HLS_URL];</span><br><span class="line">    <span class="built_in">MPMoviePlayerViewController</span> *player = [[<span class="built_in">MPMoviePlayerViewController</span> alloc] initWithContentURL:url];</span><br><span class="line">    [<span class="keyword">self</span> presentMoviePlayerViewControllerAnimated:player];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><del>当然，《芈月传》的直播链接到现在也还没有找到，各位热心读者如果有链接的话可以留言给我，也让这篇文章能实至名归了，能对得文章的标题了。</del></p>
<p>同志们，找到了官方的切片工具，已将《芈月传》切片放到了服务器上，1080P高清无码，敬请观看😁<br>具体代码我也做了更新，请大家更新代码后测试<a href="https://github.com/yangchao0033/HLS-Demo" target="_blank" rel="noopener">Demo</a>~</p>
<p>具体播放地址为:<a href="http://yangchao0033.github.io/hlsSegement/0640.m3u8" target="_blank" rel="noopener">http://yangchao0033.github.io/hlsSegement/0640.m3u8</a>(可在safari中直接观看)<br>切片工具博客：<a href="http://www.henishuo.com/hls-streaming-tool/" target="_blank" rel="noopener">http://www.henishuo.com/hls-streaming-tool/</a><br>总算不用做标题党了，摔🤓~</p>

    </div>

    
    
    
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="https://www.jianshu.com/u/f37a8f0ba6f8">
            <span class="icon">
              <i class="fas fa-book"></i>
            </span>

            <span class="label">简书</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/HLS/" rel="tag"># HLS</a>
              <a href="/tags/%E7%9B%B4%E6%92%AD/" rel="tag"># 直播</a>
              <a href="/tags/%E7%82%B9%E6%92%AD/" rel="tag"># 点播</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2016/02/13/%E4%B8%8B%E8%BD%BD%E6%8E%A7%E5%88%B6/" rel="prev" title="下载控制">
      <i class="fa fa-chevron-left"></i> 下载控制
    </a></div>
      <div class="post-nav-item">
    <a href="/2016/03/04/%E8%AE%A9-iOS-Android-%E7%BD%91%E7%BB%9CAPI%E5%BC%80%E5%8F%91%E6%9B%B4%E5%8A%A0%E8%87%AA%E7%94%B1-charles%E6%A8%A1%E6%8B%9F%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%94%E5%9B%9E%E6%95%B0%E6%8D%AE/" rel="next" title="让-iOS-Android-网络API开发更加自由-charles模拟服务器返回数据">
      让-iOS-Android-网络API开发更加自由-charles模拟服务器返回数据 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#部分源码贴出"><span class="nav-number">1.</span> <span class="nav-text">部分源码贴出</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="SuperYang"
      src="https://tva2.sinaimg.cn/crop.259.17.290.290.180/c2abdfa9jw8ez7appr3p2j20g40a2t91.jpg?KID=imgbed,tva&Expires=1594186820&ssig=BdxhDdi1Ti">
  <p class="site-author-name" itemprop="name">SuperYang</p>
  <div class="site-description" itemprop="description">牛肉炒饭不要香菜，老板收钱</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">39</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">31</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/yangchao0033" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;yangchao0033" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:ygrfwyc@gmail.com" title="E-Mail → mailto:ygrfwyc@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/yangchao0033" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;yangchao0033" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.jianshu.com/u/f37a8f0ba6f8" title="简书 → https:&#x2F;&#x2F;www.jianshu.com&#x2F;u&#x2F;f37a8f0ba6f8" rel="noopener" target="_blank"><i class="fas fa-book fa-fw"></i>简书</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://t.me/superYang0033" title="Telegram → https:&#x2F;&#x2F;t.me&#x2F;superYang0033" rel="noopener" target="_blank"><i class="fab fa-telegram fa-fw"></i>Telegram</a>
      </span>
  </div>



      </div>

      
        <div id="music163player">
        <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=350 height=86 src="//music.163.com/outchain/player?type=2&id=31473269&auto=0&height=66">
        </iframe>
        </div>
    </div>

    

  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SuperYang</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">157k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">2:23</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

  

  <script async src="/js/cursor/fireworks.js"></script>



<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : true,
      notify     : true,
      appId      : 'zizRqalUJY55Xc5oBJKbhxpV-gzGzoHsz',
      appKey     : '64gPcVi8lIA8zVGpBSEk7uKu',
      placeholder: "留言板无需注册登录，快来评论吧。。",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
